package com.fram.service.admin.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fram.bean.admin.wallet.request.WithdrawalRequest;
import com.fram.bean.api.user.response.UserWalletResponse;
import com.fram.common.canstant.ErrCode;
import com.fram.common.canstant.PublicConstant;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProRunTimeException;
import com.fram.common.exception.ProValiDataException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.encryption.AESUtils;
import com.fram.entity.UserVirtualWallet;
import com.fram.entity.UserWithdrawCardNumber;
import com.fram.mapper.UserTransactionFlowMapper;
import com.fram.mapper.UserVirtualWalletMapper;
import com.fram.mapper.UserWithdrawCardNumberMapper;
import com.fram.mapper.UserWithdrawCashListMapper;
import com.fram.service.admin.WalletService;

@Service
public class WalletServiceImpl implements WalletService {

	@Autowired
	private UserVirtualWalletMapper userVirtualWalletMapper;
	@Autowired
	private UserTransactionFlowMapper userTransactionFlowMapper;
	@Autowired
	private UserWithdrawCardNumberMapper userWithdrawCardNumberMapper;
	@Autowired
	private UserWithdrawCashListMapper userWithdrawCashListMapper;
	
	private static String key = "PAYPWD_AESUTISSS";
	
	@Override
	public UserVirtualWallet balance(Long userId) throws ProException {
		UserVirtualWallet wallet = userVirtualWalletMapper.detailForAndroid(userId);
		if (wallet == null) {
			wallet = new UserVirtualWallet();
			wallet.setUserId(userId);
			wallet.setSign(PublicMethod.getMd5(wallet.getUserId() + "0.000.00", 1, true));
			if (userVirtualWalletMapper.insertSelective(wallet) <= 0) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), "创建钱包错误");
			}
		} else {
			// 拿到流水 总增加金额 不用判断
			System.out.println("" + userId);
			UserWalletResponse wall = userTransactionFlowMapper.totalRevenue(userId);
			System.out.println("" + wall);
			wallet.setBanlanceMoney(wall.getTotalRevenue().subtract(wall.getExpenditure()));
			// 重新加密
			wallet.setSign(PublicMethod.getMd5(
					wallet.getUserId() + "" + wallet.getBanlanceMoney() + "" + wallet.getFrozenMoney(), 1, true));
			if (userVirtualWalletMapper.updateByPrimaryKeySelective(wallet) <= 0) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), "流水今天修改到钱包里出错");
			}
			if (!wallet.getSign().equals(PublicMethod.getMd5(
					wallet.getUserId() + "" + wallet.getBanlanceMoney() + "" + wallet.getFrozenMoney(), 1, true))) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), "验证不通过，请联系管理员");
			}
		}
		return wallet;

	}

	@Override
	public List<UserWithdrawCardNumber> card(Long userId) throws ProException {
		List<UserWithdrawCardNumber> list = userWithdrawCardNumberMapper.selectByUserId(userId);
		if(list == null || list.size() == 0 ) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return list;
	}

	@Override
	public void withdrawal(WithdrawalRequest request) throws ProException {
		// 钱包
		UserVirtualWallet wallet = userVirtualWalletMapper.selectByUserId(request.getUserId());
		System.out.println(""+request.getUserId());
		try {
			if (!PublicMethod.getMd5(AESUtils.encrypt(request.getPaypwd(), key), 1, true).equals(wallet.getPaypwd())) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), "请输入正确密码");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("" + wallet.getBanlanceMoney());
		if (wallet.getBanlanceMoney().compareTo(request.getWithdrawFee()) == -1) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), "提现金额超出钱包余额");
		}
		wallet.setBanlanceMoney(wallet.getBanlanceMoney().subtract(request.getWithdrawFee()));
		wallet.setSign(PublicMethod
				.getMd5(wallet.getUserId() + "" + wallet.getBanlanceMoney() + "" + wallet.getFrozenMoney(), 1, true));
		if (userVirtualWalletMapper.updateByPrimaryKeySelective(wallet) <= 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
		request.setCodeNumber(PublicMethod.getOrderNumber(PublicConstant.WITHDRAWAL_TYPE_1, null));
		request.setWithdrawStatus(1);// 发起申请
		if (userWithdrawCashListMapper.insertSelective(request) <= 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public void cardAdd(UserWithdrawCardNumber request) throws ProException {
		if(userWithdrawCardNumberMapper.insertSelective(request)<=0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public void cardUpdate(UserWithdrawCardNumber request) throws ProException {
		if(userWithdrawCardNumberMapper.updateByPrimaryKeySelective(request)<=0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public UserWithdrawCardNumber cardDetail(Long id) throws ProException {
		UserWithdrawCardNumber card= userWithdrawCardNumberMapper.selectByPrimaryKey(id);
		if(card == null) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return card;
	}

	@Override
	public void cardDelete(Long id) throws ProException {
		if(userWithdrawCardNumberMapper.deleteByPrimaryKey(id)<=0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}
}
